package com.hjimi.ski.presenter;

import android.util.Log;

import com.hjimi.ski.bean.InitDeviceBean;
import com.hjimi.ski.callback.BaseCallback;
import com.hjimi.ski.config.UrlConfig;
import com.hjimi.ski.iView.InitDeviceIView;
import com.hjimi.ski.model.base.DataModel;
import com.hjimi.ski.model.base.ModelToken;
import com.hjimi.ski.utils.JsonParseUtil;
import com.hjimi.ski.utils.RetryIntercepter;
import com.hjimi.ski.utils.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created By 孙岩
 * on 2023/8/12
 */
public class InitDevicePresenter extends BasePresenter<InitDeviceIView> {
    @SuppressWarnings("unchecked")
    public void initDevice(String mac, String sn, String ip) {
        if (isViewAttached()) {
            String url = UrlConfig.initDevice;
            DataModel.request(ModelToken.POST_REQUEST)
                    .url(url)
                    .paramKey("mac", "sn", "ip")
                    .paramValue(mac, sn, ip)
                    .execute(new BaseCallback<String>() {
                        @Override
                        public void onSuccess(String data) {

                            try {
                                JSONObject obj = new JSONObject(data);
                                if (obj.getInt("code") == 200) {
                                    JSONObject object = new JSONObject(obj.getString("data"));

                                    JSONObject turnConfigObj = new JSONObject(object.getString("turn_config"));
                                    InitDeviceBean.DataBean.TurnConfigBean turnConfigBean = new InitDeviceBean.DataBean.TurnConfigBean(
                                            JsonParseUtil.getBoolean(turnConfigObj,"is_open"),
                                            JsonParseUtil.getStr(turnConfigObj,"start_time"),
                                            JsonParseUtil.getStr(turnConfigObj,"end_time"),
                                            JsonParseUtil.getStr(turnConfigObj,"weeks")
                                    );
                                    JSONObject bgmConfigObj = new JSONObject(object.getString("bgm_config"));
                                    InitDeviceBean.DataBean.BgmConfigBean bgmConfigBean = new InitDeviceBean.DataBean.BgmConfigBean(
                                            JsonParseUtil.getBoolean(bgmConfigObj,"is_open"),
                                            JsonParseUtil.getInt(bgmConfigObj,"bgm_id"),
                                            JsonParseUtil.getStr(bgmConfigObj,"bgm_url"),
                                            JsonParseUtil.getBoolean(bgmConfigObj,"is_loop")
                                    );
                                    InitDeviceBean.DataBean bean = new InitDeviceBean.DataBean(
                                            JsonParseUtil.getStr(object, "mac"),
                                            JsonParseUtil.getStr(object, "sn"),
                                            JsonParseUtil.getInt(object, "screen_id"),
                                            JsonParseUtil.getInt(object, "size_id"),
                                            JsonParseUtil.getInt(object, "screen_angle_id"),
                                            JsonParseUtil.getInt(object, "buffer_id"),
                                            JsonParseUtil.getInt(object, "camera_angle_id"),
                                            JsonParseUtil.getInt(object, "is_camera"),
                                            JsonParseUtil.getStr(object, "ip"),
                                            JsonParseUtil.getInt(object, "is_carousel"),
                                            JsonParseUtil.getInt(object, "carousel_seconds"),
                                            JsonParseUtil.getInt(object, "is_gesture"),
                                            JsonParseUtil.getInt(object, "device_id"),
                                            JsonParseUtil.getInt(object, "device_info_id"),
                                            JsonParseUtil.getStr(object, "phone"),
                                            JsonParseUtil.getStr(object, "token"),
                                            turnConfigBean,
                                            bgmConfigBean
                                    );
                                    getView().showInitDevice(bean);
                                } else if (obj.getInt("code") == 202) {
                                    getView().show202();
                                } else {
                                    ToastUtils.show(obj.getString("msg"));
                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(String msg) {
                            getView().showErr();
                        }

                        @Override
                        public void onComplete() {

                        }

                        @Override
                        public void onBefore() {

                        }
                    });
        }

    }
}
